﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;

namespace attributeEditor
{
    public class ComboBm : ESRI.ArcGIS.Desktop.AddIns.ComboBox
    {
        IMxDocument pMxd = null;
        IMap pMap = null;
        IFeatureLayer featureLayer = null;
        ILayer layer = null;

        string editFldName = "BM";
        int editFldIdx = -1;
        private bool Enabled = false;
        string bmValue = null;
        public ComboBm()
        {
            this.Add("0110");
            this.Add("0131");
            this.Add("0132");
            this.Add("0170");
            this.Add("0311");
            this.Add("0321");
            if (ArcMap.Application.Document as IMxDocument != null)
            {
                pMxd = ArcMap.Application.Document as IMxDocument;
            }            
        }

        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }

        protected override void OnSelChange(int cookie)
        {           
            bmValue = this.Value;
            IMap map = pMxd.FocusMap;
            layer = map.get_Layer(0);
            IFeatureLayer featureLayer = layer as IFeatureLayer;
            IFeatureClass featureClass = featureLayer.FeatureClass;
            IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
            bool blOK = true;  //设blOK为false
            string sError = "";
            IWorkspace pWks = (featureClass as IDataset).Workspace;
            IWorkspaceEdit pWksEdit = pWks as IWorkspaceEdit;
            if (editFldIdx == -1)
            {
                editFldIdx = featureClass.Fields.FindField(editFldName);
            }

            bool startEdit = pWksEdit.IsBeingEdited();
            if (!startEdit)
            {
                pWksEdit.StartEditing(false);
            }
    
            pWksEdit.StartEditOperation();
            ISelectionSet idsets =featureSelection.SelectionSet;
            IEnumIDs ids = idsets.IDs;
            for (int i = 0; i < idsets.Count; i++)
            {
                int id = ids.Next();
                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = "FID =" + id;  
                IFeatureCursor featureCursor = featureClass.Search(queryFilter, false);
                IFeature feature = null;
                while ((feature = featureCursor.NextFeature()) != null)
                {
                    try
                    {
                        if (editFldIdx >= 0)
                        {
                            feature.set_Value(editFldIdx, bmValue);
                        }
                        feature.Store();
                    }
                    catch (Exception ex)
                    {
                        blOK = false;
                        sError = ex.Message;
                        break;
                    }
                    finally
                    {
                    }
                }
            }

            if (blOK)
            {
                pWksEdit.StopEditOperation();
            }
            else
            {
                pWksEdit.AbortEditOperation();
            }

            pWksEdit.StopEditing(blOK);

            if (!blOK)
            {
                MessageBox.Show("操作失败，来自异常：" + sError);
            }
        }
    }
}
